import {
    Component,
    OnInit
} from '@angular/core';
import { Location } from '@angular/common';
import { Router, ActivatedRoute } from '@angular/router';
import {Page} from '../page/page';
import construct = Reflect.construct;


@Component({
    selector: 'table_component',
    styleUrls: ['./table.component.css'],
    templateUrl: './table.component.html'
})

export class TableComponent {

    activeRouter;

    commodityList = [
        {
            commodityId: 1,
            commodityName: '榴莲酥',
            price: 10
        },
        {
            commodityId: 2,
            commodityName: '南瓜饼',
            price: 5
        },
        {
            commodityId: 3,
            commodityName: '鱼丸',
            price: 6
        },
        {
            commodityId: 4,
            commodityName: '章鱼小丸子',
            price: 6.5
        },
        {
            commodityId: 5,
            commodityName: '榴莲酥',
            price: 10
        },
        {
            commodityId: 6,
            commodityName: '南瓜饼',
            price: 5
        },
        {
            commodityId: 7,
            commodityName: '鱼丸',
            price: 6
        },
        {
            commodityId: 8,
            commodityName: '章鱼小丸子',
            price: 6.5
        },
        {
            commodityId: 9,
            commodityName: '榴莲酥',
            price: 10
        },
        {
            commodityId: 10,
            commodityName: '南瓜饼',
            price: 5
        }
    ];

    // 初始化页面参数
    pageParams = {
        pageSize: 20,
        totalNum: this.commodityList.length,
        curPage: 1,
        totalPage: Math.ceil(this.commodityList.length/20)
    };
    public ngOnInit() {
        let vm = this;
        vm.activeRouter.queryParams.subscribe(params=> {
            vm.pageParams.curPage = params.pageNo ? params.pageNo : 1;
        });
    }

    public params; // 保存页面url参数
    constructor(activeRouter:ActivatedRoute) {
        this.activeRouter = activeRouter;
        let vm = this;
        if (vm.params) {
            console.log(vm.params);
            vm.params = vm.params.replace('?', '').split('&');
            let theRequest = [];
            for (let i = 0; i < vm.params.length; i++) {
                theRequest[vm.params[i].split("=")[0]] = vm.params[i].split("=")[0] == 'pageNo' ? parseInt(vm.params[i].split("=")[1]) : vm.params[i].split("=")[1];
            }
            vm.params = theRequest;
            if (vm.params['pageNo']) {
                vm.pageParams.curPage = vm.params['pageNo'];
                console.log('当前页面', vm.pageParams.curPage);
            }
        } else {
            vm.params = {};
        }
    }

    getPageData(pageNo) {
        let vm = this;
        vm.pageParams.curPage = pageNo;
        console.log('触发', pageNo);
    }

}
